﻿using System;
using System.IO;
using System.Windows.Forms;

namespace ChangeFileTime
{
    public partial class Form1 : Form
    {
        FileInfo mFileInfo;

        public Form1()
        {
            InitializeComponent();
        }

        private void buttonSelectFile_Click(object sender, EventArgs e)
        {
            OpenFileDialog fileDlg = new OpenFileDialog();
            DialogResult clickedButton = fileDlg.ShowDialog();
            if (clickedButton != DialogResult.Cancel)
            {
                tbFilePath.Text = fileDlg.FileName;
                mFileInfo = new FileInfo(fileDlg.FileName);
                dtCreationTime.Value = mFileInfo.CreationTime;
                dtLastWriteTime.Value = mFileInfo.LastWriteTime;
                dtLastAccessTime.Value = mFileInfo.LastAccessTime;
            }
        }

        private void buttonSaveTime_Click(object sender, EventArgs e)
        {
            if (mFileInfo != null)
            {
                mFileInfo.CreationTime = dtCreationTime.Value;
                mFileInfo.LastWriteTime = dtLastWriteTime.Value;
                mFileInfo.LastAccessTime = dtLastAccessTime.Value;
            }
        }

        private void dtCreationTime_ValueChanged(object sender, EventArgs e)
        {
            DateTime creationTime = dtCreationTime.Value;
            DateTime lastWriteTime = dtLastWriteTime.Value;
            DateTime lastAccessTime = dtLastAccessTime.Value;
            dtLastWriteTime.Value = new DateTime(creationTime.Year, creationTime.Month, creationTime.Day, 
                creationTime.Hour, lastWriteTime.Minute, lastWriteTime.Second);
            dtLastAccessTime.Value = new DateTime(creationTime.Year, creationTime.Month, creationTime.Day, 
                creationTime.Hour, lastAccessTime.Minute, lastAccessTime.Second);
        }
    }
}
